package com.anjiplus.template.gaea.business.modules.websocket.controller;

import com.alibaba.fastjson.JSONObject;
import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.code.ResponseCode;
import com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto.ReportExcelDto;
import com.anjiplus.template.gaea.business.modules.websocket.dao.entity.PushData;
import com.anjiplus.template.gaea.business.modules.websocket.service.WebSocketServer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/socketC/")
public class SocketController {

    @Value("${customer.websocket-url:''}")
    private String websocketUrl;

    @RequestMapping( "/pushToWeb")
    public ResponseBean pushToWeb(@RequestBody PushData pushData) throws IOException {
        return WebSocketServer.sendInfo(pushData.getIp(),pushData.getData());
    }

    @RequestMapping("/getIp")
    public ResponseBean getIp(HttpServletRequest request) {
        if(StringUtils.isEmpty(websocketUrl)){
            return ResponseBean.builder().code(ResponseCode.FAIL_CODE).message("获取websocketUrl失败").build();
        }
        if(StringUtils.isEmpty(request.getRemoteAddr())){
            return ResponseBean.builder().code(ResponseCode.FAIL_CODE).message("获取ip地址失败").build();
        }
        Map<String,Object> data = new HashMap<>();
        data.put("websocketUrl",websocketUrl);
        data.put("remoteAddress",request.getRemoteAddr());
        return ResponseBean.builder().data(data).build();
    }

    @RequestMapping("/getIpList")
    public ResponseBean getIpList(HttpServletRequest request) {
        return ResponseBean.builder().data(WebSocketServer.getUserList()).build();
    }


}
